package com.github.tetrisgame.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.tetrisgame.utils.UIUtils;

public class AnimView extends View {
    private final Paint dotPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Path circlePath = new Path();
    private final Path mPath = new Path();
    private final int radius = UIUtils.dp2px(40);
    private int num = 15;
    private int dy = 0;
    private RectF rectF;
    private int angel1;
    private int angel2;
    private int centerY = UIUtils.dp2px(8);

    public AnimView(Context context) {
        super(context);
        init();
    }

    public AnimView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AnimView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
        invalidate();
    }

    public int getCenterY() {
        return centerY;
    }

    public void setCenterY(int centerY) {
        this.centerY = centerY;
        invalidate();
    }

    public int getAngel1() {
        return angel1;
    }

    public void setAngel1(int angel1) {
        this.angel1 = angel1;
        invalidate();
    }

    public int getAngel2() {
        return angel2;
    }

    public void setAngel2(int angel2) {
        this.angel2 = angel2;
        invalidate();
    }

    public void init() {
        dotPaint.setStyle(Paint.Style.FILL);
        dotPaint.setColor(Color.GREEN);

        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(UIUtils.dp2px(5));
        borderPaint.setColor(Color.GREEN);
        rectF = new RectF();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        circlePath.addCircle(getWidth() / 2,
                UIUtils.dp2px(220) * 3 / 4 - UIUtils.dp2px(5),
                radius,
                Path.Direction.CW);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        rectF.set(UIUtils.dp2px(10), UIUtils.dp2px(110), UIUtils.dp2px(110), UIUtils.dp2px(210));
        canvas.drawCircle(getWidth() / 2, centerY, UIUtils.dp2px(8), dotPaint);
        canvas.drawArc(rectF, -90, angel1, false, borderPaint);
        canvas.drawArc(rectF, -90, angel2, false, borderPaint);
        canvas.clipPath(circlePath);
        dy = UIUtils.dp2px(220) - UIUtils.dp2px(num);
        mPath.moveTo(getWidth() / 2 - radius, dy);
        mPath.cubicTo(getWidth() / 2 - radius / 2,
                dy - UIUtils.dp2px(20),
                getWidth() / 2 + radius / 2,
                dy + UIUtils.dp2px(20),
                getWidth() / 2 + radius,
                dy);
        mPath.lineTo(getWidth() / 2 + radius, dy + 3 * radius);
        mPath.lineTo(getWidth() / 2 - radius, dy + 3 * radius);
        mPath.close();

        canvas.drawPath(mPath, dotPaint);
        mPath.reset();
    }
}
